package com.xxx.services;

import com.xxx.mapper.DishMapper;
import com.xxx.mapper.IndentMapper;
import com.xxx.mapper.OrderMapper;
import com.xxx.mapper.ShoppingCartMapper;
import com.xxx.pojo.*;
import com.xxx.util.GetIdTime;
import com.xxx.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;

public class UserLogin {

    private Users u;//当前登入用户
    private Long id;

    private Scanner sc = new Scanner(System.in);
    private static SqlSessionFactory factory = SqlSessionFactoryUtil.getSqlSessionFactory();


    public void okLogin(Users user) {
        this.u = user;
        if (u != null) {
            if (u.getStatus() != 1) {
                System.out.println("您的账号已被禁用");
            }
            id = u.getId();
            System.out.println("欢迎" + u.getUsername() + "回来~~");
            menu();
        } else {
            System.out.println("账号或者密码错误~~~");
        }

    }

    public void menu() {
        while (true) {
            System.out.println("======用户菜单======");
            System.out.println("1.用户订餐");

            System.out.println("2.查看订单");
            System.out.println("3.删除订单");
            System.out.println("4.购物车");
            System.out.println("5.退出登入");
            System.out.println("请输入对应操作");
            switch (sc.next()) {
                case "1":
                    userOrder();
                    break;
                case "2":
                    viewOrders();
                    break;
                case "3":
                    deleteOrder();
                    break;
                case "4":
                    shoppingCart();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("输入错误，请重新输入~~");

            }
        }
    }

    /**
     * 删除订单
     */
    private void deleteOrder() {
        System.out.println("======删除订单======");
        int num;
        SqlSession sqlSession = factory.openSession();
        IndentMapper mapper = sqlSession.getMapper(IndentMapper.class);
        List<Indent> indents = mapper.selectAll();
        if (indents == null){
            System.out.println("无订单，请先订餐");
            return;
        }
        for (int i = 0; i < indents.size(); i++) {
            System.out.println((i+1) + "." + indents.get(i));
        }
        while (true){
            System.out.println("请输入序号：");
            num = sc.nextInt();
            if (num<=indents.size()){
                OrderMapper mapper1 = sqlSession.getMapper(OrderMapper.class);
                int i1 = mapper1.delByIndentId(indents.get(num - 1).getId());
                int i = mapper.deleteById(indents.get(num - 1).getId());
                if (i>0 && i1>0){
                    sqlSession.commit();
                    sqlSession.close();
                    break;
                }
            }
            else
                System.out.println("输入错误，重新输入");
        }

    }

    /**
     * 查看订单
     */
    private void viewOrders() {
        System.out.println("======查看订单======");
        SqlSession sqlSession = factory.openSession();
        IndentMapper mapper = sqlSession.getMapper(IndentMapper.class);
        List<Indent> indents = mapper.selectAll();
        if (indents == null){
            System.out.println("无订单，请先订餐");
            return;
        }
        for (int i = 0; i < indents.size(); i++) {
            System.out.println((i+1) + "." + indents.get(i));
        }
        System.out.println("任意键回到主菜单");
        sc.next();
    }

    /**
     * 订餐
     */
    private void userOrder() {

        Long id;
        Long dishId;
        Long packageId = null;
        int state;
        String createTime = GetIdTime.getDataTime();
        String payDateTime = GetIdTime.getDataTime();
        int pay = 1;
        BigDecimal price;
        String tip;
        String phone;
        String address;
        String userName;
        String dishName;

        int num;
        int a = 0,sum;
        int timeing;
        Scanner sc = new Scanner(System.in);
        SqlSession sqlSession = factory.openSession();
        DishMapper mapper = sqlSession.getMapper(DishMapper.class);
        List<Dish> dishes = mapper.selectAll();
        sqlSession.commit();
        System.out.println("菜单\n" +
                "***********************************\n" +
                "\t菜名"+"\t\t\t单价"+"\t\t\t点赞数");
        for (int i = 0; i < dishes.size(); i++) {
            System.out.println((i+1) + "\t" + dishes.get(i).getName() + "\t" + dishes.get(i).getPrice());
        }
        System.out.println("***********************************");
        while(true){
            System.out.println("订餐人姓名：");
            userName =sc.next();
            System.out.println("请选择所需菜名前的序号：");
            int number=sc.nextInt();
            while (true){
                if(number>dishes.size()){
                    System.out.println("输入错误，重新输入：");
                    number=sc.nextInt();
                }
                else {
                    dishId = dishes.get(number-1).getId();
                    dishName = dishes.get(number-1).getName();
                    break;
                }
            }


            System.out.println("当前菜品份数");
            num=sc.nextInt();
            BigDecimal b = new BigDecimal(num);
            price = dishes.get(number-1).getPrice().multiply(b);
            state = 0;//订单默认未送达
            ShoppingCart shoppingCart = new ShoppingCart(dishName,null,this.id,dishId,packageId,num,price,createTime);
            sqlSession = factory.openSession();
            ShoppingCartMapper mapper1 = sqlSession.getMapper(ShoppingCartMapper.class);

            System.out.println("订餐成功，请输入0查看购物车，输入1继续订餐");
            int i = mapper1.insertShopping(shoppingCart);
            if (i>0){
                sqlSession.commit();
                sqlSession.close();
            }
            if(sc.nextInt()==0){
                shoppingCart();
                break;
            }

        }
    }

    void shoppingCart(){
        System.out.println("======购物车======");
        SqlSession sqlSession = factory.openSession();
        ShoppingCartMapper mapper = sqlSession.getMapper(ShoppingCartMapper.class);
        List<ShoppingCart> shoppingCarts = mapper.selectAll();
        sqlSession.close();
        for (ShoppingCart shoppingCart : shoppingCarts) {
            System.out.println(shoppingCart);
        }
        System.out.println("1.结账");
        System.out.println("2.退出");
        switch (sc.next()){
            case "1":
                checkout(shoppingCarts);
                break;
            case "2":
                return;
            default:
                System.out.println("输入错误");

        }
    }

    void checkout(List<ShoppingCart> shoppingCarts){
        BigDecimal sum = new BigDecimal(0);//订单总价
        Long indentId = Long.valueOf(GetIdTime.getId());
        Order order;
        Indent indent;
        String name;
        String address;
        String phone;
        while(true) {
            System.out.println("订餐人姓名：");
            name = sc.next();
            System.out.println("所在地址：");
            address = sc.next();
            System.out.println("电话号码：");
            phone = sc.next();
            if (phone.length() == 11){
                break;
            }
        }
        for (ShoppingCart shoppingCart : shoppingCarts) {
            sum =sum.add(shoppingCart.getPrice());
        }

        indent = new Indent(indentId,2,this.id,GetIdTime.getDataTime(),1,sum,null,phone,address,name);
        SqlSession sqlSession1 = factory.openSession();
        IndentMapper mapper1 = sqlSession1.getMapper(IndentMapper.class);
        int i1 = mapper1.insertIndent(indent);



        for (ShoppingCart shoppingCart : shoppingCarts) {
            order = new Order(shoppingCart.getDishName(),shoppingCart.getImg(),indentId,shoppingCart.getDishId(),shoppingCart.getPackageId(),null,shoppingCart.getNum(),shoppingCart.getPrice());
            SqlSession sqlSession = factory.openSession();
            OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
            mapper.insertOrder(order);
            sqlSession.commit();
            sqlSession.close();
        }
        SqlSession sqlSession = factory.openSession();
        ShoppingCartMapper mapper = sqlSession.getMapper(ShoppingCartMapper.class);
        int i = mapper.delAll();
        if (i1>0){
            sqlSession1.commit();
            sqlSession1.close();
        }
        if (i>0){
            sqlSession.commit();
            sqlSession.close();
        }
    }
}
